/**
 * @author jstone
 */

var oppView = new KONtx.Class({
 	ClassName: 'oppView',
	
	Extends: KONtx.system.SidebarView,
	
	config: {
	    visibleRows: 4,
	},
	createView: function() {
	    var myUrl = new URL;
	    myUrl.location= "http://" + host + "/matchup.php";
	    this.data = JSON.parse(myUrl.fetch());
	    
	    this.backButton   = new KONtx.control.BackButton({ label: 'Back to Main Menu' }).appendTo(this);
	    this.topContainer = new KONtx.control.EmptySpace({
		    styles: {
          height: this.height - this.backButton.height,
          vOffset: this.backButton.height,
          }
        }).appendTo(this);
	    this.controls.pageIndicator = new KONtx.control.PageIndicator({
        id: 'team.PageIndicator',
        threshold: 8,
        styles: {
          vAlign: 'bottom',
          vOffset: this.topContainer.height,
        }
      }).appendTo(this.topContainer);
    
	    
	    this.controls.listGrid = new KONtx.control.Grid({
		    columns: 1,
		    rows: this.config.visibleRows,
		    carousel: true,
		    cellCreator: this._cellCreator,
		    cellUpdater: this._cellUpdater,
		    styles: {
          vOffset: 0 ,
          width: this.width,
			    height: this.topContainer.height - this.controls.pageIndicator.height
        },
		  }).appendTo(this.topContainer);
      
      this.controls.listGrid.attachAccessory(this.controls.pageIndicator);
	    
	},
	
	_cellCreator: function() {
	    var cell = new KONtx.control.GridCell({
		    styles: {
			height: KONtx.utility.scale(35),
				width: this.width,
		    },
		    events: {
			onSelect: function(event) {
					log("You selected Button with original 'i' # of: " + this.originalButtonNumber);
			}
		    }
		});
	    cell.playerImage = new KONtx.element.Image({
		    height:65,
		    width:45,
		    styles: {
			vAlign: "center",
			hAlign: "left",
		    }
		}).appendTo(cell);
	    
		cell.name = new KONtx.element.Text({
			styles: {
			    color: '#FFFFFF',
			    fontSize: KONtx.utility.scale(14),
			    hOffset:85,
			    vOffset:20,
			    height:20,
			    width: 150}
			
		    }).appendTo(cell);
	    cell.position = new KONtx.element.Text({
		    styles: {
			color: '#FFFFFF',
			fontSize: KONtx.utility.scale(18),
			hOffset:cell.name.width + 85,
			vOffset:20,
			height:20,
			width: 40}
		}).appendTo(cell);
	    cell.projected = new KONtx.element.Text({
		    styles: {
			color: '#33CCC0',
			fontSize: KONtx.utility.scale(18),
			hOffset: cell.name.width + cell.position.width + 80,
			vOffset:20,
			height:20,
			width: this.height - cell.name.width - cell.position.width - 85}
		}).appendTo(cell);
	    
	    cell.info = new KONtx.element.Text({
		    styles: {
			color: '#FFFF33',
			fontSize: KONtx.utility.scale(12),
			hOffset: 85,
			vOffset:20 + cell.name.height + 16,
			height:30,
			width: 120
		    }
		}).appendTo(cell);
	    cell.actual = new KONtx.element.Text({
		    styles: {
			color: '#FF0000',
			fontSize: KONtx.utility.scale(18),
			hOffset: cell.name.width + cell.position.width + 80,
			vOffset:20 + cell.projected.height + 16,
			height:30,
			width: this.height - cell.name.width - cell.position.width - 85}
		}).appendTo(cell);
	    
	    
	    return cell;
	},
	
	_cellUpdater: function(cell, dataitem) {
	    if (this.bye == true){
		cell.name.data = "BYE";
		cell.info.setText("");
		cell.projected.setText("-");
		cell.actual.setText("-");
		//		cell.playerImage.setSource();
	    }
	    cell.name.data = dataitem.name;
	    cell.info.setText(dataitem.description);
	    cell.projected.setText(dataitem.projected);
	    cell.actual.setText(dataitem.actual);
	    cell.playerImage.setSource(dataitem.url);
	    cell.originalButtonNumber = dataitem.buttonNumber;
	},
	
	updateView: function() {
	            var arr = [];
		    var playerkey;
		    var info;
        for (var i = 0; i < 12; i++) {
	    playerkey = "player" + i;
	    info = this.data[1][playerkey].playerPos + " - " + 
		this.data[1][playerkey].playerTeam + 
		" vs. " + 
		this.data[1][playerkey].location;
	    if (this.data[1][playerkey].location == "Bye"){
		this.bye = true;
	    }
	    
	    arr.push({
		    name: this.data[1][playerkey].playerName,
			url: this.data[1][playerkey].playerUrl,
			projected: this.data[1][playerkey].playerProjected,
			actual: this.data[1][playerkey].playerActual,
			description: info,
			buttonNumber: i,
			});
	}
		    this.controls.listGrid.changeDataset(arr);
	}
    });



